﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using JaernCloud.Language.Compiler.Types.Builtin.Model;
using JaernCloud.Language.Compiler.Types;

namespace JaernCloud.Language.Runtime.Model {
    
    public class UserNodeObject : DynamicObject, INodeObject<UserNodeType> {
        
        readonly int _id = NodeObjectExtensions.NewId;
        public int Id { get { return _id; } }

        private UserNodeType _type;
        private INodeObject<ModelNodeType> _parent;
        private INodeObjectGenerator _nodeObjectGenerator;


        #region Dynamic

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            switch (binder.Name) {
                case "roles":
                    // TODO: Return real user roles
                    result = new List<String> { "Admin", "Test 1", "Test 2"};
                    return true;
            }
            return base.TryGetMember(binder, out result);
        }

        #endregion

        #region INodeObject<UserNodeType> Members

        public UserNodeType Type {
            get { return _type; }
        }

        public INodeObject<UserNodeType> FromType(INodeType type, INodeObjectGenerator objectGenerator) {
            return new UserNodeObject { _type = (UserNodeType)type, _nodeObjectGenerator = objectGenerator };
        }

        public IEnumerable<INodeObject<INodeType>> Children {
            get {
                yield break;
            }
        }

        public INodeObject<INodeType> Parent {
            get {
                return _parent;
            }
            set {
                if (value is INodeObject<ModelNodeType>) {
                    _parent = value as INodeObject<ModelNodeType>;
                } else {
                    throw new ArgumentException("Parent type must be " + typeof(ModelNodeType));
                }
            }
        }

        public string Name {
            get { return _type.Name; }
        }

        public IEnumerable<string> Names {
            get { return _type.Names; }
        }

        public IEnumerable<object> Values {
            get {
                   yield break;
            }
        }

        #endregion
    }
}
